package com.IT.servletJsp.loginRegister;

import com.IT.service.Impl.UserServiceImpl;
import com.IT.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(value = "/loginJsp")
public class LoginJsp extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset = utf-8");

        PrintWriter printWriter = response.getWriter();

        long account = 0;
        try {
            account = Long.parseLong(request.getParameter("account"));
        }catch (Exception e){
            //printWriter.print("账号输入非法，请检查！！");
            printWriter.print("<script>alert('账号输入非法，请检查！！');window.location.href='index1.html'</script>");
        }
        String password = request.getParameter("password");

        UserService userService = new UserServiceImpl();

        int authority = userService.login(account, password);


        String  ip = InetAddress.getLocalHost().toString();

        Date dNow = new Date();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String loginTime = ft.format(dNow);

        HttpSession session = request.getSession();
        if(authority == 3){
            userService.insertLog(account, "登录成功", ip, loginTime);

            session.setAttribute("account", account);
            response.sendRedirect("adminInterface.html");
        }
        else if(authority == 2){
            userService.insertLog(account, "登录成功", ip, loginTime);

            session.setAttribute("account", account);
            response.sendRedirect("doctorInterface.html");
        }
        else if(authority == 1){
            userService.insertLog(account, "登录成功", ip, loginTime);

            session.setAttribute("account", account);
            response.sendRedirect("patientInterface.html");
        }
        else if(authority == 0){
            userService.insertLog(account, "登录失败", ip, loginTime);

            //数据库操作异常
            printWriter.print("<script>alert('登录异常！');window.location.href='index1.html'</script>");
        }
        else if(authority == -1){
            userService.insertLog(account, "登录失败", ip, loginTime);
            //无此账号
            printWriter.print("<script>alert('不存在此账号，请检查！');window.location.href='index1.html'</script>");
        }
        else if(authority == -2){
            userService.insertLog(account, "登录失败", ip, loginTime);
            //密码错误
            printWriter.print("<script>alert('密码错误！');window.location.href='index1.html'</script>");
        }
        else {
            userService.insertLog(account, "登录失败", ip, loginTime);
            //无意义
            printWriter.print("登录失败！！");
        }
    }
}
